const BaseController = require("./base");
const qs  = require('qs')
const StatErrorModel = require('../modules/stat_error')
const StatBehavior = require('../modules/stat_behavior')
const StatPerformance = require('../modules/stat_performance')
const StatService = require('../service/stat')

module.exports = class StatController extends BaseController {
  async sagif(ctx) {
    let datas = []
    if (ctx.request.method == 'GET') {
      datas = qs.parse(ctx.request.url.split('?')[1]).datas
    } else {
      datas = JSON.parse(ctx.request.body)
    }
    // console.log(ctx.request.headers)
    // 判断是否有反向代理 IP
    let ip = ctx.ip || ctx.ips.join(',')
    let ua = ctx.request.headers['user-agent']

    let datasEnrich = datas.map(item => {
      item.ip = ip
      item.ua = ua
      item.time = new Date(item.time)
      return item
    })
    if (!datasEnrich.length) {
      ctx.body = 'ok'
      return
    }
    if (datasEnrich[0].type == 'error') {
      await StatErrorModel.addBatch(datasEnrich)
    }
    if (datasEnrich[0].type == 'behavior') {
      await StatBehavior.addBatch(datasEnrich)
    }
    if (datasEnrich[0].type == 'performance') {
      await StatPerformance.addBatch(datasEnrich)
    }
    ctx.body = true
  }

  async getPvLimit(ctx) {
    const oneDay = 24 * 60 * 60 * 1000
    let { days = 1, ip, limit = 10 } = qs.parse(ctx.request.url.split('?')[1])
    let date = new Date(Date.now() - oneDay * days)
    let res = await StatBehavior.getPvLimit({ date, ip, limit: parseInt(limit) })
    ctx.body = res
  }

  async getIpVisitList(ctx) {
    const oneDay = 24 * 60 * 60 * 1000
    let { days = 1, limit = 10 } = qs.parse(ctx.request.url.split('?')[1])
    let date = new Date(Date.now() - oneDay * days)
    let res = await new StatService().getIpVisitList({ date, limit: parseInt(limit) })
    ctx.body = res
  }

  async getIpList(ctx) {
    const oneDay = 24 * 60 * 60 * 1000
    let { days = 1 } = qs.parse(ctx.request.url.split('?')[1])
    let date = new Date(Date.now() - oneDay * days)
    let res = await StatBehavior.getIpList({ date })
    ctx.body = res
  }

  async getErrorList(ctx) {
    let { pageIndex, pageSize } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatErrorModel.getErrorList({ offset: (pageIndex - 1) * pageSize, limit: parseInt(pageSize) })
    ctx.body = res
  }

  async getRemainList(ctx) {
    let { pageIndex, pageSize } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatBehavior.getRemainList({ offset: (pageIndex - 1) * pageSize, limit: parseInt(pageSize) })
    ctx.body = res
  }
  async getClickList(ctx) {
    let { pageIndex, pageSize } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatBehavior.getClickList({ offset: (pageIndex - 1) * pageSize, limit: parseInt(pageSize) })
    ctx.body = res
  }
  async getLoadList(ctx) {
    let { pageIndex, pageSize } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatPerformance.getLoadList({ offset: (pageIndex - 1) * pageSize, limit: parseInt(pageSize) })
    ctx.body = res
  }
  async getResourceLoadList(ctx) {
    let { pageIndex, pageSize } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatPerformance.getResourceLoadList({ offset: (pageIndex - 1) * pageSize, limit: parseInt(pageSize) })
    ctx.body = res
  }
  async getHourGroup(ctx) {
    let { date } = qs.parse(ctx.request.url.split('?')[1])
    let res = await StatBehavior.getHourGroup({ date })
    ctx.body = res
  }

}